package com.liuxing.javaSpringBoot.modules.account.dao;

import com.liuxing.javaSpringBoot.modules.account.entity.Role;
import com.liuxing.javaSpringBoot.modules.common.entity.SearchBean;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * @File RoleDao.java
 * @Auth liuxing
 * @Date 2021/8/23 16:42
 * @Email liuxing997@foxmail.com
 */
@Mapper
@Repository
public interface RoleDao {

    //添加角色信息
    @Insert("insert into account_role (create_date,role_name)" +
            "values(#{createDate},#{roleName})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    void insertRole(Role role);

    //根据角色ID修改角色信息
    @Update("update account_role set role_name = #{roleName}" +
            "where id = #{id}")
    void updateRoleById(Role role);

    //根据角色ID删除角色
    @Delete("delete from account_role where id = #{id}")
    void deleteRoleByRoleId(int id);

    //根据角色ID查询角色
    @Select("select * from account_role where id=#{id}")
    Role getRoleByRoleId(int id);

    //根据用户ID查询角色信息
    @Select("select * from account_role ar left join account_user_role aur on ar.id = aur.role_id "
            + "where aur.user_id = #{userId}")
    List<Role> getRolesByUserId(int userId);

    //根据资源ID查询角色信息
    @Select("select * from account_role ar left join account_role_resource arr on ar.id = arr.role_id "
            + "where arr.resource_id = #{resourceId}")
    List<Role> getRolesByResourceId(int resourceId);

    //查询所有角色信息
    @Select("select * from account_role")
    List<Role> getAllRole();

    //批量插入角色信息
    @Insert("<script>"
            + "insert into account_role (create_date,role_name)" +
            " values <foreach collection='roles' item='role' index='index' separator=','>"
            + "(#{role.createDate},#{role.roleName})</foreach>"
            + "</script>")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    void insertRoles(@Param("roles") List<Role> roles);

    //分页查询角色信息
    @Select("<script>"
            + "select * from account_role "
            + "<where> "
            + "<if test='keyword != \"\" and keyword != null'>"
            + " and (role_name like '%${keyword}%' "
            + "</if>"
            + "</where>"
            + "<choose>"
            + "<when test='orderBy != \"\" and orderBy != null'>"
            + " order by ${orderBy} ${direction}"
            + "</when>"
            + "<otherwise>"
            + " order by id desc"
            + "</otherwise>"
            + "</choose>"
            + "</script>")
    List<Role> getRolesBySearchBean(SearchBean searchBean);
}
